#!/bin/bash
hosts='127.0.0.1'
users=root
passwds='123456'
dbname='test'
zonefile=/root/zone/zone.txt
sqlfile=/root/zone/sql.txt
logfile=/root/zone/del_foods.log
echo "---------------------------------------------------------" >> $logfile
echo $(date +"%y-%m-%d %H:%M:%S") >> $logfile
echo "---------------------------------" >> $logfile
for n in `cat ${zonefile}`
do
    mysql  -h ${hosts} -u${users} -p${passwds} -e "use ${dbname};select ordercode from msc_sso_${n}foods_orders  where orderstate='0';" | grep -v ordercode > $sqlfile
    echo "------------------------------------------------------"
    for i in  `cat ${sqlfile}`
    do
        echo "`date '+%Y-%m-%d_%H.%M'`    OK   ${n}   ${i}" >> ${logfile}
        mysql -u${users} -p{passwds} -e "use ${dbname};delete from msc_sso_${n}foods_orders_detail  where ordercode='$i'" > /dev/null 2>&1
        if [[ $? -eq 0 ]];then
            echo "`date '+%Y-%m-%d_%H.%M'`    OK   ${n}   ${i}" >> ${logfile}
        else
            echo "`date '+%Y-%m-%d_%H.%M'`   Fail   ${n}   ${i}" >> ${logfile}
        fi
    done
    mysql -u${users} -p${passwds} -e "use ${dbname};delete from msc_sso_${n}foods_orders  where orderstate='0';"
done
